#include "MBDistanceMap.h"

MBDistanceMap::MBDistanceMap( void ) :
	m_data( 0 ),
	m_heightPixels( 0 ),
	m_widthPixels( 0 )
{

}

float MBDistanceMap::GetDepthAtCoord( double u, double v, bool bottomLeftOrigin ) const
{
	if (!m_data)
		return 0.0;

	bool a = !bottomLeftOrigin; // top-left origin for texture-coords
	bool b = true;				// top-left origin in image -> true for m_data

	// if (a xor b) => image origin and texture coordinates origin differ
	if ((a && !b) || (!a && b) ) 
	{
		// flip the vertical axis for the texture coordinate
		v = 1.0 - v;
	}

	MBuint x = static_cast<MBuint>( u * m_widthPixels );
	MBuint y = static_cast<MBuint>( v * m_heightPixels );

	if (x >= m_widthPixels) x = m_widthPixels - 1;
	if (y >= m_heightPixels) y = m_heightPixels - 1;

	return m_data[y * m_widthPixels + x];
}